package com.study.constant;

import lombok.Getter;

/**
 * @author canace
 * @version 1.0
 * @description 缓存常量
 * @date 2024/1/27 16:06
 */
public class CacheConstant {

    /**
     * 本项目 Redis 缓存前缀
     */
    public static final String REDIS_CACHE_PREFIX = "Cache::shortLink::";

    /**
     * Caffeine 缓存管理器
     */
    public static final String CAFFEINE_CACHE_MANAGER = "caffeineCacheManager";

    /**
     * Redis 缓存管理器
     */
    public static final String REDIS_CACHE_MANAGER = "redisCacheManager";

    /**
     * 根据用户名获取用户信息
     */
    public static final String USER_INFO_BY_USERNAME_NAME = "userInfoByUsernameCache";
    /**
     * 根据gid获取分组信息
     */
    public static final String GROUP_INFO_BY_GID_CACHE_NAME = "groupInfoByGidCache";
    /**
     * 根据用户名获取分组列表
     */
    public static final String GROUP_LIST_BY_USERNAME_CACHE_NAME = "groupListByUsernameCache";
    /**
     * 根据gid获取短链接数量(这里保存的不是单独一个gid，而是一个查询的集合)
     */
    public static final String LINK_COUNT_BY_GID_CACHE_NAME = "linkCountByGidCache";

    public static final String LINK_STATS_BY_SHORT_LINK_NAME = "linkStatsByShortLinkCache";

    public static final String LINK_STATS_BY_GROUP_NAME = "linkStatsByGroupCache";


    /**
     * 缓存配置常量
     */
    public enum CacheEnum {
        USER_INFO_BY_USERNAME_CACHE(2, USER_INFO_BY_USERNAME_NAME, 60 * 60 * 24, 1000),
        GROUP_INFO_BY_GID_CACHE(2, GROUP_INFO_BY_GID_CACHE_NAME, 60 * 60 * 24, 1000),
        GROUP_LIST_BY_USERNAME_CACHE(2, GROUP_LIST_BY_USERNAME_CACHE_NAME, 60 * 60 * 24, 1000),
        LINK_COUNT_BY_GID_CACHE(2, LINK_COUNT_BY_GID_CACHE_NAME, 60 * 60 * 24, 1000),
        LINK_STATS_BY_SHORT_LINK_CACHE(2, LINK_STATS_BY_SHORT_LINK_NAME, 60 * 60 * 24 * 7, 1000),
        LINK_STATS_BY_GROUP_CACHE(2, LINK_STATS_BY_GROUP_NAME, 60 * 60 * 24 * 7, 1000);


        /**
         * 缓存类型 0-本地 1-本地和远程 2-远程
         */
        private final int type;
        /**
         * 缓存的名字
         */
        @Getter
        private final String name;
        /**
         * 失效时间（秒） 0-永不失效
         */
        @Getter
        private final int ttl;
        /**
         * 最大容量（仅对caffeine有效）
         */
        @Getter
        private final int maxSize;

        CacheEnum(int type, String name, int ttl, int maxSize) {
            this.type = type;
            this.name = name;
            this.ttl = ttl;
            this.maxSize = maxSize;
        }

        public boolean isLocal() {
            return type <= 1;
        }

        public boolean isRemote() {
            return type >= 1;
        }
    }
}
